package com.mindao.app.site.demo;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mindao.app.ea.demo.InfDemo;
import com.mindao.app.ea.demo.ParentDemo;
import com.mindao.app.ea.model.Organize;
import com.mindao.app.ea.model.Resource;
import com.mindao.app.ea.model.Role;
import com.mindao.app.ea.model.RolePosition;
import com.mindao.app.ea.model.User;
import com.mindao.app.ea.service.RoleService;
import com.mindao.app.weixin.demo.ImpEaMinDao;

@Component("impDemoWeixin")
public class ImpDemoWeixin extends ParentDemo implements InfDemo{
	private final Logger log = LoggerFactory.getLogger(ImpDemoWeixin.class);
	@Autowired
	protected RoleService roleService;

	@Autowired
	protected ImpEaMinDao impEaMindao;

	public void testData() throws Exception {
		//用户初始化
		if(baseDao.find(" from User ").size()==0){
			
			log.debug("初始化数据");
			//用户初始化
			User admin = create_user("admin", "系统管理员", "1324567890","admin@qq.com", "");//管理所有
			User test = create_user("test", "test", "18688385892","test@qq.com", "");//只能编辑企业软文：微信、微博
			
 
			//部门初始化
			//Saas管理多个公司
			Organize oMindao = create_organize("敏道科技公司", "mdkj",null );
//			Organize oMeichen = create_organize("美臣文化公司", "mcwh",null );
//			
//			//某个大公司多个子部门
//			Organize kjds = create_organize("某跨境电商公司", "xx", null);		
//			Organize mtb = create_sub_organize(kjds,"媒体部", "mtb",null );
//			Organize qudao = create_sub_organize(kjds,"渠道代理商", "qudao",null );
//			
//			Organize gd = create_sub_organize(qudao,"广东省", "gd",null );
//			Organize hn = create_sub_organize(qudao,"湖南省", "hn",null );
//			Organize bj = create_sub_organize(qudao,"北京市", "bj",null );
//			Organize sh = create_sub_organize(qudao,"上海市", "sh",null );

			//职位初始化
			RolePosition ceo = create_RolePosition( "CEO");
//			RolePosition dlzg = create_RolePosition( "代理总管");
//			RolePosition siteAdmin = create_RolePosition( "网站管理员");
//			RolePosition wxAdmin = create_RolePosition( "微信管理员");
			RolePosition employee = create_RolePosition( "普通员工");
 
			//角色初始化
			Role adminRole=createOrgEmptyRole("系统管理员",null);
 
			
			//敏道公司角色："所有人员"下有"网站管理员"和"微信管理员"
			Role role_mindao_ceo=createOrganizeRole(oMindao.getId().toString() , ceo.getId().toString(),null);
			Role role_mindao_all=createOrganizeRole(oMindao.getId().toString() , employee.getId().toString(),role_mindao_ceo);
//			Role role_mindao_siteAdmin=createOrganizeRole(oMindao.getId().toString() , siteAdmin.getId().toString(),role_mindao_ceo);
//			Role role_mindao_wxAdmin=createOrganizeRole(oMindao.getId().toString() , wxAdmin.getId().toString(),role_mindao_ceo);
			
//			//美臣公司角色："所有人员"下有"网站管理员"和"微信管理员"
//			Role role_meichen_ceo=createOrganizeRole(oMeichen.getId().toString() , ceo.getId().toString(),null);
//			role_meichen_ceo.setName(role_meichen_ceo.getName().replace("-"+ceo.getName(), ""));
//			baseDao.update(role_meichen_ceo);			
//			Role role_meichen_all=createOrganizeRole(oMeichen.getId().toString() , employee.getId().toString(),role_meichen_ceo);
//			Role role_meichen_siteAdmin=createOrganizeRole(oMeichen.getId().toString() , siteAdmin.getId().toString(),role_meichen_ceo);
//			Role role_meichen_wxAdmin=createOrganizeRole(oMeichen.getId().toString() , wxAdmin.getId().toString(),role_meichen_ceo);
//			
//			//跨境电商角色：不同部门下可能有各自的“网站管理员”或“微信管理员”
//			Role role_kjds_ceo=createOrganizeRole(kjds.getId().toString() , ceo.getId().toString(),null);
//			role_kjds_ceo.setName(role_kjds_ceo.getName().replace("-"+ceo.getName(), ""));
//			baseDao.update(role_kjds_ceo);
//			Role role_kjds_all=createOrganizeRole(kjds.getId().toString() , employee.getId().toString(),role_kjds_ceo);
//			Role role_kjds_mtb_siteAdmin=createOrganizeRole(mtb.getId().toString() , siteAdmin.getId().toString(),role_kjds_ceo);
//			Role role_kjds_mtb_wxAdmin=createOrganizeRole(mtb.getId().toString() , wxAdmin.getId().toString(),role_kjds_ceo);
//			
//			
//			Role role_kjds_dlzg=createOrganizeRole(qudao.getId().toString() , dlzg.getId().toString(),role_kjds_ceo);
//			role_kjds_dlzg.setName(role_kjds_dlzg.getName().replace("-"+dlzg.getName(), ""));
//			baseDao.update(role_kjds_dlzg);
//			
//			Role role_kjds_gd_siteAdmin=createOrganizeRole(gd.getId().toString() , siteAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_gd_wxAdmin=createOrganizeRole(gd.getId().toString() , wxAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_hn_siteAdmin=createOrganizeRole(hn.getId().toString() , siteAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_hn_wxAdmin=createOrganizeRole(hn.getId().toString() , wxAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_bj_siteAdmin=createOrganizeRole(bj.getId().toString() , siteAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_bj_wxAdmin=createOrganizeRole(bj.getId().toString() , wxAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_sh_siteAdmin=createOrganizeRole(sh.getId().toString() , siteAdmin.getId().toString(),role_kjds_dlzg);
//			Role role_kjds_sh_wxAdmin=createOrganizeRole(sh.getId().toString() , wxAdmin.getId().toString(),role_kjds_dlzg);
 
			//资源初始化
			Resource r0 = create_resource("系统资源", "systemAdmin", null,false,Resource.TYPE_OTHERS);
//			Resource r01 = create_sub_resource(r0, "站点编辑","cmsAdmin",null,false,Resource.TYPE_MENU);
			
			Resource r02 = create_sub_resource(r0, "微信管理","softAdmin",null,true,Resource.TYPE_MENU);
			//Resource r03 = create_sub_resource(r0, "微博博文管理","weiboAdmin",null,false,Resource.TYPE_MENU);			
			

			
			Resource r00 = create_sub_resource(r0,"系统管理", "systemAdmin", null,false,Resource.TYPE_MENU);
			Resource r001 = create_sub_resource(r00, "组织管理","organizeAdmin","ea/organize/menu_tree.do",false,Resource.TYPE_MENU);
			Resource r002 = create_sub_resource(r00, "角色管理","roleAdmin","ea/role/menu_tree.do",false,Resource.TYPE_MENU);
			Resource r003 = create_sub_resource(r00, "资源管理","resourceAdmin","ea/resource/menu_tree.do",false,Resource.TYPE_MENU);
			Resource r004 = create_sub_resource(r00, "用户管理","userAdmin","ea/user/search.do",false,Resource.TYPE_MENU);
			
			Resource r004_c = create_sub_resource(r004, "增加","userAdmin_create","ea/user/create.do",false,Resource.TYPE_FUNCTION);
			Resource r004_u = create_sub_resource(r004, "修改","userAdmin_edit","ea/user/edit.do?id=",false,Resource.TYPE_MENU);
			Resource r004_d = create_sub_resource(r004, "删除","userAdmin_delete","ea/user/delete.do",false,Resource.TYPE_MENU);
			Resource r004_p = create_sub_resource(r004, "修改密码","userAdmin_passwd","ea/user/passwd.do",false,Resource.TYPE_MENU);
			//Resource r04_s = create_sub_resource(r04, "保存","userAdmin_save","ea/user/save.do",false,Resource.TYPE_MENU);
			//Resource r04_pwd = create_sub_resource(r04, "保存密码","userAdmin_sumbitPwd","ea/user/sumbitPwd.do",false,Resource.TYPE_MENU);
			
			Resource r005 = create_sub_resource(r00, "权限管理","permissionAdmin","ea/permission/menu_role_res.do",false,Resource.TYPE_MENU);
			Resource r006 = create_sub_resource(r00, "流程管理","workflowAdmin","activiti/activiti/menu_process_definition.do",false,Resource.TYPE_MENU);
			//Resource r006 = create_sub_resource(r0, "邮件服务器设置 ","smtp_menu_smtp_init", "ea_smtp_menu_smtp_init.do");
			Resource r007 = create_sub_resource(r00, "系统参数 ","systemparaAdmin", "ea/systempara/search.do",false,Resource.TYPE_MENU);
			
//			Resource r008 = create_sub_resource(r0, "网站站群开通","siteAdmin",null,false,Resource.TYPE_MENU);
//			Resource r009 = create_sub_resource(r0, "微信账号开通","wxAdmin",null,false,Resource.TYPE_MENU);
			
			
			//资源授权
			Set<Long> adminResIdSet=new HashSet<>();
			adminResIdSet.add(r0.getId());
//			adminResIdSet.add(r01.getId());
			adminResIdSet.add(r02.getId());
			
			adminResIdSet.add(r00.getId());
			adminResIdSet.add(r001.getId());
			adminResIdSet.add(r002.getId());
			adminResIdSet.add(r003.getId());
			adminResIdSet.add(r005.getId());
			adminResIdSet.add(r006.getId());
			adminResIdSet.add(r007.getId());
//			adminResIdSet.add(r008.getId());
//			adminResIdSet.add(r009.getId());
			
		 
			adminResIdSet.add(r004.getId());
			adminResIdSet.add(r004_c.getId());
			adminResIdSet.add(r004_u.getId());
			adminResIdSet.add(r004_d.getId());
			adminResIdSet.add(r004_p.getId());
			
 		
 
			//网站管理员的资源:站点编辑
//			Set<Long>  siteAdminResSet=new HashSet<>();
//			siteAdminResSet.add(r01.getId());
			
			//微信管理员的资源:软文编辑，菜单不做控制，每个人进入后都可看到本公司的所有文章，都可编辑文章，只控制发布权限。用户--->角色---->用这个角色的建立的微信账号
  
			try {
				//系统管理员授权
				String amdinResIds= adminResIdSet.toString().replace("[", "").replace("]", "");
				roleService.grantResources(adminRole.getId().toString(), amdinResIds,"");
				roleService.grantUsers(adminRole.getId().toString(), admin.getId().toString());
 
//				//敏道站点管理员授权,分配用户给admin
			
			}catch (Exception e){
				log.error(e.toString() );
			}
			

			
		}
	}

	@Override
	public void initData() throws Exception {
		testData();
		//敏道公众号菜单初始化
		impEaMindao.initData();
	}
	
	
}
